<template>
  <input
    type="text"
    :rules="rules"
    :msg="msg"
    :class="{'ipt':true,'success':statu,'error':!statu}"
    @input="data"
    @blur="hdblur"
  />
</template>

<script>
export default {
  props: ["rules", "msg"],
  data() {
    return {
      // 标记用户的输入是否合法
      statu: true,
    };
  },
  methods: {
    hdblur(e) {
      let value = e.target.value;
      if (this.rules && this.rules.test(value)) {
        this.statu = true;
        this.$emit("statu", true);
      } else {
        this.statu = false;
        this.$emit("statu", false);
        this.$toast.fail(this.msg);
      }
    },
    data(e) {
      let value = e.target.value;
      this.$emit("input", value);
    },
  },
};
</script>

<style lang="less" scoped>
.ipt {
  width: 318/360 * 100vw;
  height: 60px;
  outline: none;
  border: none;
  border-bottom: 3px solid #ccc;
  font-size: 20px;
  line-height: 60px;
}
.success {
  border-bottom-color: green;
}
.error {
  border-bottom-color: red;
}
</style>